<template>
  <div class="register-container">
    <div class="register-box">
      <!-- 左侧品牌介绍 -->
      <div class="intro-box">
        <div class="header-section">
          <h1 class="welcome-text">欢迎加入云盘系统</h1>
          <p class="system-intro">开启您的安全云存储之旅</p>
        </div>

        <div class="features-grid">
          <div
              v-for="(feature, index) in features"
              :key="index"
              class="feature-card"
          >
            <el-icon :size="32" class="feature-icon">
              <component :is="feature.icon" />
            </el-icon>
            <div class="feature-content">
              <h3 class="feature-title">{{ feature.title }}</h3>
              <p class="feature-description">{{ feature.description }}</p>
            </div>
          </div>
        </div>
      </div>

      <!-- 右侧注册表单 -->
      <div class="form-section">
        <div class="form-wrapper">
          <div class="form-header">
            <h2 class="form-title">用户注册</h2>
            <p class="form-subtitle">创建您的云盘账户</p>
          </div>

          <el-form ref="formRef" :model="data.form" :rules="data.rules">
            <!-- 用户名 -->
            <el-form-item prop="username">
              <el-input
                  v-model="data.form.username"
                  :prefix-icon="User"
                  placeholder="请输入用户名"
                  size="large"
              />
            </el-form-item>

            <!-- 邮箱 -->
            <el-form-item prop="email">
              <el-input
                  v-model="data.form.email"
                  :prefix-icon="Message"
                  placeholder="请输入邮箱"
                  size="large"
              />
            </el-form-item>

            <!-- 手机号 -->
            <el-form-item prop="phone">
              <el-input
                  v-model="data.form.phone"
                  :prefix-icon="Iphone"
                  placeholder="请输入手机号"
                  size="large"
              />
            </el-form-item>

            <!-- 密码 -->
            <el-form-item prop="password">
              <el-input
                  v-model="data.form.password"
                  :prefix-icon="Lock"
                  placeholder="请输入密码"
                  show-password
                  size="large"
              />
            </el-form-item>

            <!-- 确认密码 -->
            <el-form-item prop="confirmPassword">
              <el-input
                  v-model="data.form.confirmPassword"
                  :prefix-icon="Lock"
                  placeholder="请确认密码"
                  show-password
                  size="large"
              />
            </el-form-item>

            <div class="form-actions">
              <el-button
                  type="primary"
                  size="large"
                  class="register-btn"
                  @click="register"
              >
                立即注册
              </el-button>
              <div class="login-link">
                <span>已有账户？</span>
                <router-link to="/login" class="link-text">立即登录</router-link>
              </div>
            </div>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>


<script setup>
import { reactive, ref } from "vue";
import { Lock, User, Message, Iphone } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
import request from "@/utils/Request";
import {registerApi} from "@/utils/API/auth";
import {mkdirAuthApi} from "@/utils/API/hdfs";

const router = useRouter();

// 特性数据
const features = [
  { icon: 'Files', title: '文件存储', description: '海量存储空间，安全可靠' },
  { icon: 'UploadFilled', title: '多端同步', description: '支持多设备实时同步，随时随地访问' },
  { icon: 'Unlock', title: '安全保障', description: '高级加密技术，保护您的隐私' },
  { icon: 'Share', title: '共享协作', description: '便捷的文件共享与团队协作功能' }
]

// 密码验证
const validatePass = (rule, value, callback) => {
  if (!value) {
    callback(new Error('请确认密码'))
  } else if (value !== data.form.password) {
    callback(new Error('两次输入密码不一致'))
  } else {
    callback()
  }
}

// 邮箱验证
const validateEmail = (rule, value, callback) => {
  const emailReg = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/
  if (!value) {
    callback(new Error('请输入邮箱地址'))
  } else if (!emailReg.test(value)) {
    callback(new Error('邮箱格式不正确'))
  } else {
    callback()
  }
}

// 手机号验证
const validatePhone = (rule, value, callback) => {
  const phoneReg = /^1[3-9]\d{9}$/
  if (!value) {
    callback(new Error('请输入手机号'))
  } else if (!phoneReg.test(value)) {
    callback(new Error('手机号格式不正确'))
  } else {
    callback()
  }
}

const data = reactive({
  form: {
    role: 'USER',
    username: '',
    email: '',
    phone: '',
    password: '',
    confirmPassword: ''
  },
  rules: {
    username: [
      { required: true, message: '请输入用户名', trigger: 'blur' },
      { min: 4, max: 16, message: '长度在4到16个字符', trigger: 'blur' }
    ],
    email: [
      { validator: validateEmail, trigger: 'blur' }
    ],
    phone: [
      { validator: validatePhone, trigger: 'blur' }
    ],
    password: [
      { required: true, message: '请输入密码', trigger: 'blur' },
      { min: 6, max: 18, message: '密码长度6-18位', trigger: 'blur' }
    ],
    confirmPassword: [
      { validator: validatePass, trigger: 'blur' }
    ]
  }
})

const formRef = ref()

const register = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      registerApi(data.form)
          .then(res => {
            if (res.code === 200) {
              ElMessage.success("注册成功")
              router.push('/login')
            } else {
              ElMessage.error(res.msg)
            }
          })
          .catch(error => {
            console.error(error)
            ElMessage.error("注册失败，请检查网络连接")
          })
    }
  })
}
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  padding: 2rem;
}

.register-box {
  display: flex;
  background: white;
  border-radius: 1.5rem;
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.1);
  max-width: 1200px;
  width: 100%;
  min-height: 680px;
  overflow: hidden;
}

/* 左侧品牌介绍 */
.intro-box {
  flex: 1;
  padding: 3rem;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  display: flex;
  flex-direction: column;
}

.header-section {
  margin-bottom: auto;
}

.welcome-text {
  font-size: 2.2rem;
  line-height: 1.3;
  margin-bottom: 1rem;
}

.system-intro {
  font-size: 1.1rem;
  opacity: 0.9;
}

/* 右侧注册表单 */
.form-section {
  flex: 0 0 45%;
  padding: 4rem 3rem;
  display: flex;
  align-items: center;
}

.form-wrapper {
  width: 100%;
  max-width: 400px;
}

.form-header {
  text-align: center;
  margin-bottom: 2.5rem;
}

.form-title {
  font-size: 1.8rem;
  margin-bottom: 0.5rem;
}

.form-subtitle {
  color: #666;
  font-size: 0.95rem;
}

/* 表单元素 */
:deep(.el-input__wrapper) {
  padding: 0.8rem 1rem;
  border-radius: 10px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
}

.register-btn {
  width: 100%;
  font-weight: 500;
  letter-spacing: 0.5px;
  height: 48px;
  transition: all 0.3s ease;
  margin-top: 1rem;
}

.login-link {
  text-align: center;
  margin-top: 1.5rem;
  color: #666;
}

.link-text {
  color: #409eff;
  transition: color 0.3s;
  text-decoration: none;
  margin-left: 0.5rem;
}

.link-text:hover {
  color: #66b1ff;
}

/* 响应式设计 */
@media (max-width: 992px) {
  .register-box {
    flex-direction: column;
    min-height: auto;
  }

  .intro-box {
    padding: 2rem;
    border-radius: 1.5rem 1.5rem 0 0;
  }

  .form-section {
    padding: 3rem 2rem;
  }

  .features-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 576px) {
  .register-container {
    padding: 1rem;
  }

  .welcome-text {
    font-size: 1.8rem;
  }

  .form-section {
    padding: 2rem 1.5rem;
  }
}
</style>